<template>
	<div class="demo-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item label="代码">
					<el-input v-model="state.queryParams.code" placeholder="代码" clearable />
				</el-form-item>
				<el-form-item label="名称">
					<el-input v-model="state.queryParams.name" placeholder="名称" clearable />
				</el-form-item>
				<el-form-item label="描述">
					<el-input v-model="state.queryParams.description" placeholder="描述" clearable />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddDemo" v-auth="'demo:add'"> 新增 </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<el-table :data="state.demoData" style="width: 100%" v-loading="state.loading" border>
				<el-table-column type="index" label="序号" width="55" align="center" />
				<el-table-column prop="code" label="代码" width="150" align="center" show-overflow-tooltip />
				<el-table-column prop="name" label="名称" width="150" align="center" show-overflow-tooltip />
				<el-table-column prop="description" label="描述" min-width="200" align="center" show-overflow-tooltip />
				<el-table-column prop="createTime" label="创建时间" width="180" align="center" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.createTime }}
					</template>
				</el-table-column>
				<el-table-column prop="updateTime" label="更新时间" width="180" align="center" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.updateTime }}
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200" align="center" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-tooltip content="编辑" placement="top">
							<el-button icon="ele-Edit" text type="primary" @click="openEditDemo(scope.row)" v-auth="'demo:edit'"> </el-button>
						</el-tooltip>
						<el-tooltip content="删除" placement="top">
							<el-button icon="ele-Delete" text type="danger" @click="delDemo(scope.row)" v-auth="'demo:delete'"> </el-button>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:currentPage="state.tableParams.pageNo"
				v-model:page-size="state.tableParams.pageSize"
				:total="state.tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				size="small"
				background
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
			/>
		</el-card>

		<EditDemo ref="editDemoRef" :title="state.editDemoTitle" @handleQuery="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="demoIndex">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditDemo from './component/editDemo.vue';

// 导入API接口，这里使用模拟数据，实际项目中需要替换为真实API
import { apiDemoPagePost, apiDemoDeletePost } from '/@/api-services/api';

const editDemoRef = ref<InstanceType<typeof EditDemo>>();
const state = reactive({
	loading: false,
	demoData: [] as Array<any>,
	queryParams: {
		name: undefined,
		description: undefined
	},
	tableParams: {
		pageNo: 1,
		pageSize: 20,
		total: 0 as any,
	},
	editDemoTitle: '',
});

onMounted(async () => {
	await handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	try {
		let params = Object.assign(state.queryParams, state.tableParams);
		let res = await apiDemoPagePost(params);
		state.demoData = res.data.data?.rows ?? [];
		state.tableParams.total = res.data.data?.totalRows;
	} catch {
		ElMessage.error('查询失败');
	} finally {
		state.loading = false;
	}
};

// 重置操作
const resetQuery = async () => {
	state.queryParams.name = undefined;
	state.queryParams.description = undefined;
	await handleQuery();
};

// 打开新增页面
const openAddDemo = () => {
	state.editDemoTitle = '新增Demo';
	editDemoRef.value?.openDialog({ id: undefined, name: '', description: '' });
};

// 打开编辑页面
const openEditDemo = (row: any) => {
	state.editDemoTitle = '编辑Demo';
	editDemoRef.value?.openDialog(row);
};

// 删除
const delDemo = (row: any) => {
	ElMessageBox.confirm(`确定删除【${row.name}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			try {
				await apiDemoDeletePost({ id: row.id });
				await handleQuery();
				ElMessage.success('删除成功');
			} catch {
				ElMessage.error('删除失败');
			}
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = async (val: number) => {
	state.tableParams.pageNo = val;
	await handleQuery();
};
</script>

<style scoped>
.demo-container {
	padding: 10px;
}
</style>